<?php
require_once 'db/singletonDB.php';

$db = singletonDB::getInstance();

/**查询一条数据
$where = [
    "merchant_id[=]" => "20005",
    "ORDER" => [
        "id" => "DESC",
    ],
    
];
$columns = ['id','order_id(orderid)','merchant_id(merid)'];
$obj = $db->get("order", $columns, $where);
print_r($obj);**/

/**查询
$columns = ['id','order_id(orderid)','merchant_id(merid)'];
$where = [
    "AND" => [
        "id[>]" => 200,
        "order_id[~]" => ["2021082105"],
        "merchant_id[!]" => "20005"
    ]
];
$list = $db->select('order', $columns, $where);
print_r($list);**/

/**分页查询
$columns = ['id','order_id(orderid)','merchant_id(merid)'];
$where = [
    "AND" => [
        "merchant_id[=]" => "20005"
    ],
    "LIMIT" => [200,10],
];
$list = $db->select('order', $columns, $where);
print_r($list);**/

/**新增
$data = [];
$data['status'] = rand(-1,3);
$data['merchant_id'] = rand(10000,11000);
$data['order_id'] = date('YmdHis').rand(1000,9999);
$data['orgin_id'] = rand(10000,99999);
$data['site'] = 'baidu.com';
$data['order_amount'] = rand(1000,20000);
$data['current'] = 'USD';
$data['order_rmb'] = $data['order_amount']*6.45;
$data['order_time'] = time();
$db->insert("order", $data);
$res = $db->errorInfo;
if(empty($res)){//成功
    $last_order_id = $db->id();
    echo '订单id:'.$last_order_id;
    //订单id:11323557
}else{//失败
    echo $res[0].'=='.$res[1].'=='.$res[2];
    //42S22==1054==Unknown column 'pay_typ' in 'field list'
}**/

/**修改
$data = [];
$data['status'] = 1;
$data['luck'] = 8100;
$where = [
    'id' => 10000
];
$result = $db->update('order', $data, $where);
$res = $db->errorInfo;
if($result->rowCount() > 0){//成功
    echo '更新成功:'.$result->rowCount();
}else{
    if(empty($res)){//更新失败
        echo '未更新成功';
    }else{//操作失败
        echo $res[0].'=='.$res[1].'=='.$res[2];
        //42S22==1054==Unknown column 'luck1' in 'field list'
    }
}**/


/***删除
$where = [
    'id' => 11323557
];
$result = $db->delete('order', $where);
$res = $db->errorInfo;
if($result->rowCount() > 0){//成功
    echo '删除成功:'.$result->rowCount();
    //更新成功
}else{//失败
    if(empty($res)){
        echo '删除失败';
    }else{
        echo $res[0].'=='.$res[1].'=='.$res[2];
        //42S22==1054==Unknown column 'luck1' in 'field list'
    }
}***/

/**总条数
$where = [
    "merchant_id[=]" => "10005"
];
$count = $db->count("order", $where);
echo $count;**/

/***事物处理
$db->action(function($db) {
    $db->insert("admin", [
        "name" => "admin",
        "pwd" => "123456"
    ]);
    $res1 = $db->error;
    
    $db->delete("order", [
        "id" => 11323556
    ]);
    $res2 = $db->error;
    echo $db->last();
    if (!empty($res1) || !empty($res2))
    {
        echo '操作失败,事务回滚:'.$res1.'==='.$res2;
        return false;
    }else{
        echo '操作成功';
    }
});***/

/**
$data = "Medoo";
echo "We love " . $data.'<br/>'; // We love Medoo
echo "We love " . $db->quote($data); // We love 'Medoo'
**/
//echo uniqid('order_');
